﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using System.Data;
using AutoMapper;

namespace WaiGaoQiaoWeb.Controllers
{
    public class TempCareUnitManagementController : Controller
    {
        private TempCareUnitManagementInfo tempinfo = new TempCareUnitManagementInfo(true);
        public ActionResult TempCareUnitManagementIndex()
        {
            return View();
        }

        /// <summary>
        /// 获取临时卡单位列表
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetList(PageVM pvm)
        {
            string query = " where 1=1 ";
            if (string.IsNullOrEmpty(pvm.query))
            {
                query += " and 1=1";
            }
            else
            {
                string param = pvm.query;
                if (param.ToString() != "")
                {
                    query += " and deptName like '%" + param + "%'";
                }
            }
            IList<TempCareUnitManagementInfo> templist = tempinfo.SelTempCardUnitInfo(pvm.rp, pvm.page, query);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = tempinfo.GetTempCardUnitcount(query);
            List<string> v;
            foreach (TempCareUnitManagementInfo item in templist)
            {
                #region
                v = new List<string>();
                v.Add(item.Dep_ID.ToString());
                v.Add(item.Dep_No);
                v.Add(item.deptName.ToString());
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.Dep_ID.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

        public ActionResult AddTempCardUnit()
        {
            return View();
        }

        /// <summary>
        /// 保存临时卡单位（新增界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveTempCardUnitAdd(TempCareUnitManagementVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                TempCareUnitManagementInfo temp = new TempCareUnitManagementInfo()
                {
                    deptName = model.deptName
                };
                result = tempinfo.SaveTempCardUnitAdd(temp);
            }
            return Json(result);
        }

        /// <summary>
        /// 删除临时卡单位
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public ActionResult DeleteTempCardUnit(string id)
        {
            int result = 0;
            result = tempinfo.DeleteTempCardUnit(id);
            return Json(result);
        }

        /// <summary>
        /// 点击修改按钮执行的方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult EditTempCardUnit(string id)
        {
            TempCareUnitManagementVM vm = new TempCareUnitManagementVM();
            TempCareUnitManagementInfo info = new TempCareUnitManagementInfo();
            tempinfo = info.GetTempCardUnitInfoOne(id);
            vm.Dep_ID = tempinfo.Dep_ID;
            vm.deptName = tempinfo.deptName;
            return View(vm);
        }

        /// <summary>
        /// 保存临时卡单位（修改界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveTempCardUnitEdit(TempCareUnitManagementVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                TempCareUnitManagementInfo temp = new TempCareUnitManagementInfo()
                {
                    Dep_ID = model.Dep_ID,
                    deptName = model.deptName
                };
                result = tempinfo.SaveTempCardUnitEdit(temp);
            }
            return Json(result);
        }
    }
}
